<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

/**
*
* @package MediaLibrary
* @copyright 2009 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);Rob de Cleen(rob@decleen.com)
* Homepage: http://www.joomlawebserver.com
* @version: 1.5.3 Basic $
*
**/




class mosMediaLibrary_lend_request extends mosDBTable {
	/** @var int Primary key */
	var $id = null;
	/** @var int - the media id this lend is assosiated with*/
	var $fk_mediaid=null;	
	/** @var int - the user id of the user who lend this media can also be null if $user_name is set*/
	var $fk_userid=null;
	/** @var datetime - since when this media is lend out*/
	var $lend_from=null;
	/** @var datetime - when the media should be returned*/
	var $lend_until=null;
	/** @var datetime - when the media realy was/is returned*/
	var $lend_request=null;
	/** @var boolean */
	var $checked_out=null;
	/** @var time */
	var $checked_out_time=null;
	/** @var string - the user who lent this media if it's no user of the database*/
	var $user_name=null;
	/** @var string – the name of the user who lent this media if it's no user of the database*/
	var $user_email=null;
	/** @var string – the e-mail address of the user who lent this media if it's no user of the database*/
	var $user_mailing=null;
	/** @var string – the e-mail address of the user who lent this media if it's no user of the database*/
  var $status=null;
	
	/**
	* @param database - A database connector object
	*/
	function mosMediaLibrary_lend_request( &$db ) {
		$this->mosDBTable( '#__medialibrary_lend_request', 'id', $db );
	}
	
	// overloaded check function
	function check() {
		
		// check if media is already lent out
		$this->_db->setQuery( "SELECT fk_lendid FROM #__medialibrary "
			. "\nWHERE id='$this->fk_mediaid' AND fk_lendid = null"
		);						
		$xid = intval( $this->_db->loadResult() );
		if ($xid) {
			$this->_error = "This media already lend out";
			return false;
		}
		return true;
	}
	
		
	/**
	 * @return array – name: the string of the user the media is lent to - e-mail: the e-mail address of the user
	 */
	function getLendTo(){
		$retVal['name'] = null;
		$retVal['email'] = null;
		if($this->fk_userid != null && $this->fk_userid != 0){
			$this->_db->setQuery( "SELECT name, email from #__users where id=$this->fk_userid");
			$help = $this->_db->loadRow();
			$retVal['name'] = $help[0];
			$retVal['email'] = $help[1];
		}
		else{
			$retVal['name'] = &$user_name;
			$retVal['email'] = &$user_email;
		}
		return $retVal;
	}
	
	//status codes
	//0: just inserted
	//1: accepted
	//2: not accepted
	
	function accept(){
		global $my;
		if($this->id == null){
			return "Method called on a non instant object";
		}
		$this->checkout($my->id);
		
		//create new lend dataset
		$lend = new mosMediaLibrary_lend($this->_db);
		$media = new mosMediaLibrary($this->_db);
		$media->checkout($my->id);
		$media->load($this->fk_mediaid);
		if($media->fk_lendid != 0){
			return "Media already lent out!";
		}
		$lend->lend_from = date("Y-m-d H:i:s");
		$lend->fk_mediaid=$this->fk_mediaid;
		$lend->userid=$this->userid;
		$lend->until=$this->until;
		$lend->user_name=$this->user_name;
		$lend->user_email=$this->user_email;
		$lend->user_mailing=$this->user_mailing;
		
		$lend->lend_from = date("Y-m-d H:i:s");
		$lend->lend_until = $this->lend_until;		
		
		if (!$lend->check($lend)) {
			return $lend->getError();
		}
		if (!$lend->store()) {
			return $lend->getError();
		}

		$lend->checkin();
		//update media with lend id		
		$media->fk_lendid = $lend->id;
		if (!$media->store()) {
			return $media->getError();
		}
		$media->checkin();
		
		$this->status = 1;

		if (!$this->store()) {
			return $this->getError();
		}
		$this->checkin();
		return null;
	}
	
	function decline(){
		if($this->id == null){
			return "Method called on a non instant object";
		}
		$this->status = 2;
		if (!$this->store()) {
			return $this->getError();
		}
		return null;
	}
	
	function toXML(& $xmlDoc){
		
		//create and append name element 
		$retVal = & $xmlDoc->createElement("lendrequest"); 
		
		$fk_userid = & $xmlDoc->createElement("fk_userid");
		$fk_userid->appendChild($xmlDoc->createTextNode($this->fk_userid));
		$retVal->appendChild( $fk_userid);
		
		$lend_from = & $xmlDoc->createElement("lend_from");
		$lend_from->appendChild($xmlDoc->createTextNode($this->lend_from));
		$retVal->appendChild( $lend_from);
		
		$lend_until = & $xmlDoc->createElement("lend_until");
		$lend_until->appendChild($xmlDoc->createTextNode($this->lend_until));
		$retVal->appendChild( $lend_until);
		
		$lend_request = & $xmlDoc->createElement("lend_retquest");
		$lend_request->appendChild($xmlDoc->createTextNode($this->lend_request));
		$retVal->appendChild( $lend_request);
		
		$user_name = & $xmlDoc->createElement("user_name");
		$user_name->appendChild($xmlDoc->createTextNode($this->user_name));
		$retVal->appendChild( $user_name);
		
		$user_email = & $xmlDoc->createElement("user_email");
		$user_email->appendChild($xmlDoc->createTextNode($this->user_email));
		$retVal->appendChild( $user_email);
		
		$user_mailing = & $xmlDoc->createElement("user_mailing");
		$user_mailing->appendChild($xmlDoc->createTextNode($this->user_mailing));
		$retVal->appendChild( $user_mailing);
		
		$status = & $xmlDoc->createElement("status");
		$status->appendChild($xmlDoc->createTextNode($this->status));
		$retVal->appendChild( $status);
		
		return $retVal;
	}
}
?>
